/* gcc -lrt */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <mqueue.h>

#include "msg.h"

int main(int argc, char *argv[])
{
    mqd_t fd, ret;
    const char *message_0 = "Hello World!";
    
    /* /dev/mqueue/filename as the message queue */
    fd = mq_open(argv[1], O_RDWR, 0666,NULL); 
    if(fd == -1) {
        ERR_EXIT("producer: mq_open()");
    } 
    
    //将message_0放入消息队列中,并指定大小和优先级.
    //attr.mq_msgsize=TEXT_SIZE 会被consumer接收到.
    //test cases: 放入四条消息:
    int count = 0;
    for(int i = 0;i<3;i++){
        //前两条为优先级2的消息
        if(i<2){
            ret = mq_send(fd,message_0,TEXT_SIZE,2);
            if(ret == -1){
                ERR_EXIT("producer:mq_send()");
            }
        }else{
            //最后一条为优先级3的消息；
            ret = mq_send(fd,message_0,TEXT_SIZE,3);
            if(ret == -1){
                ERR_EXIT("producer:mq_send()");
            }
        }
        count++;
    }

    printf("produced %d message.\n", count);
    return EXIT_SUCCESS;
}
